package org.wx.herostory.services.encoder;

import io.netty.channel.ChannelHandlerContext;
import org.wx.herostory.msg.GameMsgProtocol;
import org.wx.herostory.services.rank.RankItem;
import org.wx.herostory.services.rank.RankService;

/**
 * @description:
 * @author: wangxun
 * @createDate: 2021/3/8
 * @version: 1.0
 */
public class GetRankCmdHandler implements ICmdHandler<GameMsgProtocol.GetRankCmd>{
    @Override
    public void handle(ChannelHandlerContext ctx, GameMsgProtocol.GetRankCmd cmd) {
        if(null ==  ctx || null ==cmd){
            return;
        }

        RankService.getInstance().getRank((rankItemList)->{
            // 新建rankResult的build
            final GameMsgProtocol.GetRankResult.Builder builder = GameMsgProtocol.GetRankResult.newBuilder();
            // 排行榜每一条数据
            for (RankItem rankItem : rankItemList) {
                GameMsgProtocol.GetRankResult.RankItem.Builder itemBuilder =
                        GameMsgProtocol.GetRankResult.RankItem.newBuilder();

                itemBuilder.setRankId(rankItem.getRankId());
                itemBuilder.setUserId(rankItem.getUserId());
                itemBuilder.setUserName(rankItem.getUserName());
                itemBuilder.setHeroAvatar(rankItem.getHeroAvatar());
                itemBuilder.setWin(rankItem.getWin());

                builder.addRankItem(itemBuilder);
            }

            ctx.writeAndFlush(builder.build());
            return null;
        });
    }
}
